package kbs.scm
{
    import flash.utils.getDefinitionByName;

    public class NewInstanceFunction extends PrimitiveFunction
    {
        public function NewInstanceFunction(cname:String)
        {
            m_cname = cname;
        }

        public function getName():String
        { return m_cname; }

        override public function scmapply(alist:AList, env:Env):Base
        {
            var myc:Class = getDefinitionByName(m_cname) as Class;
            var argc:int = alist.length();
            var a:Array = [];
            for (var i:int = 0; i<argc; i++) {
                a.push(toObject(alist.get(i), env));
            }

            var myret:Object;
            switch (argc) {
            case 0: myret = new myc(); break;
            case 1: myret = new myc(a[0]); break;
            case 2: myret = new myc(a[0], a[1]); break;
            case 3: myret = new myc(a[0], a[1], a[2]); break;
            case 4: myret = new myc(a[0], a[1], a[2], a[3]); break;
            case 5: myret = new myc(a[0], a[1], a[2], a[3], a[4]); break;
            case 6: myret = new myc(a[0], a[1], a[2], a[3], a[4], a[5]); break;
            case 7: myret = new myc(a[0], a[1], a[2], a[3], a[4], a[5], a[6]); break;
            case 8: myret = new myc(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]); break;
            default:
                throw new Error("Oops -- out of hack arguments!");
            }

            return toBase(myret);
        }

        override public function pprint():String
        {
            return m_cname+".";
        }
        private var m_cname:String;
    }
}
